package cn.iocoder.yudao.module.member.controller.admin.recruitCompany;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.SortingField;
import cn.iocoder.yudao.framework.common.util.number.NumberUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.member.controller.admin.company.vo.*;
import cn.iocoder.yudao.module.member.controller.admin.recruitCompany.vo.*;
import cn.iocoder.yudao.module.member.convert.company.CompanyConvert;
import cn.iocoder.yudao.module.member.convert.recruitCompany.RecruitCompanyConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.company.CompanyDO;
import cn.iocoder.yudao.module.member.dal.dataobject.recruitCompany.JobAndInvitCountDo;
import cn.iocoder.yudao.module.member.dal.dataobject.recruitCompany.JobPassDo;
import cn.iocoder.yudao.module.member.dal.dataobject.recruitCompany.RecruitCompanyDO;
import cn.iocoder.yudao.module.member.service.company.CompanyService;
import cn.iocoder.yudao.module.member.service.recruitCompany.RecruitCompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 -运营端- 招聘企业管理")
@RestController
@RequestMapping("/recruitCompany")
@Validated
public class RecruitCompanyController {

    @Resource
    private RecruitCompanyService recruitCompanyService;

    @Resource
    private CompanyService companyService;

    @PostMapping("/companyList")
    @ApiOperation("获得招聘企业分页")
    @TenantIgnore
    public CommonResult<PageResult<RecruitCompanyRespVO>> selectCompanyJobList(@Valid @RequestBody RecruitCompanyPageReqVO pageVO) {
        if(null!=pageVO.getIsHot()&& 1==pageVO.getIsHot()){
            SortingField s=new SortingField();
            s.setField("o.hotWeight");
            s.setOrder("desc");
            pageVO.setSortField(s);
        }
        if(null!=pageVO.getIsNew()&& 1==pageVO.getIsNew()){
            SortingField s=new SortingField();
            s.setField("o.newWeight");
            s.setOrder("desc");
            pageVO.setSortField(s);
        }
        if(null==pageVO.getSortField()){
            SortingField s=new SortingField();
            s.setField("o.updateTime");
            s.setOrder("desc");
            pageVO.setSortField(s);
        }
        PageResult<RecruitCompanyDO> pageResult = recruitCompanyService.getRecruitCompanyPage(pageVO);
        return success(RecruitCompanyConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出招聘企业 Excel")
    @OperateLog(type = EXPORT)
    public void exportCompanyExcel( HttpServletResponse response) throws IOException {
//        List<CompanyDO> list = companyService.getCompanyList(exportReqVO);
        RecruitCompanyPageReqVO pageVO=new RecruitCompanyPageReqVO();
        SortingField s=new SortingField();
        s.setField("o.updateTime");
        s.setOrder("desc");
        pageVO.setSortField(s);
        pageVO.setExportFlag("1");
        PageResult<RecruitCompanyDO> pageResult = recruitCompanyService.getRecruitCompanyPage(pageVO);
        List<MemberCompanyExcelVO> data =new ArrayList<>();
        if(pageResult.getTotal()>0){
            List<RecruitCompanyDO> list1 = pageResult.getList();
            list1.stream().forEach(m->{
                MemberCompanyExcelVO company=new MemberCompanyExcelVO();
                BeanUtils.copyProperties(m,company);
                if(StringUtils.isNotBlank(m.getStatus()) && "-1".equals(m.getStatus())){
                    company.setStatus("启用");
                }else{
                    company.setStatus("禁用");
                }
                data.add(company);
            });
        }
        ExcelUtils.write(response, "招聘企业.xls", "数据", MemberCompanyExcelVO.class, data);
    }

    @PutMapping("/updateRankingStatus")
    @ApiOperation("设置最新(热门)企业置顶")
    @TenantIgnore
    public CommonResult<Boolean> updateRankingStatus(@RequestBody RecruCompanyUpdateReqVO updateReqVO ) {
        if(null==updateReqVO.getId()||null==updateReqVO.getIsHot()||null==updateReqVO.getIsNew()){
            return error("参数错误");
        }
        recruitCompanyService.updateCompanyNew(updateReqVO.getId(),updateReqVO.getIsNew());
        recruitCompanyService.updateCompanyHot(updateReqVO.getId(),updateReqVO.getIsHot());
        return success(true);
    }


    @GetMapping("/getHotCompanyList")
    @ApiOperation("获得热门(最新)招聘企业置顶")
    @TenantIgnore
    @ApiImplicitParam(name = "type", value = "企业类型:1:热门企业,2:最新企业", required = true, dataTypeClass = Integer.class)
    public CommonResult<List<RecruitCompanyDO>> getHotCompanyList(@RequestParam("type") Integer type ) {
        RecruitCompanyPageReqVO pageVO=new RecruitCompanyPageReqVO();
        if(1==type){
            pageVO.setIsHot(1);
            SortingField sortField=new SortingField();
            sortField.setField("hot_weight");
            sortField.setOrder("desc");
            pageVO.setSortField(sortField);
        }else{
            pageVO.setIsNew(1);
            SortingField sortField=new SortingField();
            sortField.setField("new_weight");
            sortField.setOrder("desc");
            pageVO.setSortField(sortField);
        }
        List<RecruitCompanyDO> rankingCompanyList = recruitCompanyService.getRankingCompanyList(pageVO);
        return success(rankingCompanyList);
    }

    @GetMapping("/updateCompanyNew")
    @ApiOperation("修改招聘最新企业置顶状态")
    @TenantIgnore
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "公司ID", required = true, dataTypeClass = Long.class),
            @ApiImplicitParam(name = "isNew", value = "最新职位置顶(1:是,0:否)",  dataTypeClass = Integer.class)
    })
    public CommonResult<Boolean> updateCompanyNew(Long id,Integer isNew) {
        recruitCompanyService.updateCompanyNew(id,isNew);
        return success(true);
    }

    @GetMapping("/updateCompanyHot")
    @ApiOperation("修改招聘热门企业置顶状态")
    @TenantIgnore
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "公司ID", required = true, dataTypeClass = Long.class),
            @ApiImplicitParam(name = "isHot", value = "热门职位置顶(1:是,0:否)",  dataTypeClass = Integer.class)
    })
    public CommonResult<Boolean> updateCompanyHot(Long id,Integer isHot) {
        recruitCompanyService.updateCompanyHot(id,isHot);
        return success(true);
    }

    @PutMapping("/updateRankingCompanySort")
    @ApiOperation("企业置顶排序")
    @TenantIgnore
    //@PreAuthorize("@ss.hasPermission('member:news-study-info:delete')")
    public CommonResult<Boolean> updateRankingCompanySort(@Valid @RequestBody RankingCompanyIdReqVO rankingIdReqVO ) {
        recruitCompanyService.updateRankingCompanySort(rankingIdReqVO);
        return success(true);
    }



    @PostMapping("/setCompanyStatus")
    @ApiOperation("修改招聘企业状态")
    @TenantIgnore
    public CommonResult<Boolean> setCompanyStatus(@Valid  @RequestBody  RecruCompanyUpdateReqVO recruCompanyUpdateReqVO) {
        recruitCompanyService.setCompanyStatus(recruCompanyUpdateReqVO);
        return success(true);
    }

    @PostMapping("/setCompanyTop")
    @ApiOperation("招聘企业置顶")
    @TenantIgnore
    public CommonResult<Boolean> setCompanyTop(@Valid  @RequestBody  RecruCompanyUpdateReqVO recruCompanyUpdateReqVO) {
        recruitCompanyService.setCompanyTop(recruCompanyUpdateReqVO);
        return success(true);
    }

    @GetMapping("/checkCompany")
    @ApiOperation("获得企业管理详情")
    @TenantIgnore
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<CompanyRespVO> getCompany(@RequestParam("id") Long id) {
        CompanyDO company = recruitCompanyService.getCompany(id);
        return success(CompanyConvert.INSTANCE.convert(company));
    }



    @PostMapping("/getHotCompanyJobList")
    @ApiOperation("获得热门招聘企业TOP10")
    @TenantIgnore
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "startDate",value ="开始时间",dataType ="Date" ),
            @ApiImplicitParam(paramType = "query",name = "endDate",value ="结束时间",dataType ="Date" )
    })
    public CommonResult<List<RecruitCompanyRespVO>> getHotCompanyJobList( Date startDate, Date endDate) {
        List<RecruitCompanyDO> listResult = recruitCompanyService.getHotCompanyJobList(startDate,endDate);
        return success(RecruitCompanyConvert.INSTANCE.convertList(listResult));
    }

    @PostMapping("/getCompanyInfoCount")
    @ApiOperation("运营-招聘统计(招聘企业)")
    @TenantIgnore
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "query",name = "type",value ="查询类型,0:默认(不传时间),1:时间间隔,2:本周,3:本月",dataType ="int" ),
        @ApiImplicitParam(paramType = "query",name = "startDate",value ="开始时间",dataType ="string" ),
        @ApiImplicitParam(paramType = "query",name = "endDate",value ="结束时间",dataType ="string" )
    })
    public CommonResult<CompanyInfoRespVO> getCompanyInfoCount(Integer type,String startDate,String endDate) {
        CompanyInfoRespVO companyInfoCount = recruitCompanyService.getCompanyInfoCount(NumberUtils.getInteger(type,0), startDate, endDate);
        return success(companyInfoCount );
    }


    @PostMapping("/getJobAndinvitCount")
    @ApiOperation("获得招聘统计数据")
    @TenantIgnore
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "startDate",value ="开始时间",dataType ="Date" ),
            @ApiImplicitParam(paramType = "query",name = "endDate",value ="结束时间",dataType ="Date" )
    })
    public CommonResult<JobAndInvitCountDo> getJobAndinvitCount( Date startDate, Date endDate) {
       JobAndInvitCountDo jobAndinvitCount = recruitCompanyService.getJobAndinvitCount(startDate,endDate);
        return success(jobAndinvitCount);
    }
    @GetMapping("/getJobAndinvitCountNew")
    @ApiOperation("新获得招聘统计数据")
    @TenantIgnore
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "type",value ="查询类型,0:默认(不传时间),1:时间间隔,2:本周,3:本月",dataType ="int",required = true),
            @ApiImplicitParam(paramType = "query",name = "dayFlowFlag",value ="是否剔除日增流量(1:剔除,0:不剔除)",dataType ="int" ),
            @ApiImplicitParam(paramType = "query",name = "startDate",value ="开始时间",dataType ="string" ),
            @ApiImplicitParam(paramType = "query",name = "endDate",value ="结束时间",dataType ="string" )
    })
    public CommonResult<JobAndInvitCountDo> getJobAndinvitCountNew( Integer type,Integer dayFlowFlag,
                                                                     String startDate,String endDate) {
        if(null==type ){
            return error("类型不能为空");
        }
        dayFlowFlag=NumberUtils.getInteger(dayFlowFlag,0);
        if(1==dayFlowFlag){
            String loginUserName = WebFrameworkUtils.getLoginUserName();
            if(!"admin".equals(loginUserName)){
                return error("只有超级管理员才能查看剔除系统日增流量的数据");
            }
        }
        JobAndInvitCountDo jobAndinvitCount = recruitCompanyService.getJobAndinvitCountNew(type,dayFlowFlag,startDate,endDate);
        return success(jobAndinvitCount);
    }

    @PostMapping("/getPassJobList")
    @ApiOperation("获得招聘通过top10数据")
    @TenantIgnore
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "startDate",value ="开始时间",dataType ="String" ),
            @ApiImplicitParam(paramType = "query",name = "endDate",value ="结束时间",dataType ="String" )
    })
    public CommonResult<List<JobPassRespVo>> getPassJobList( String startDate, String endDate) {
        List<JobPassDo> feedBackDoList = recruitCompanyService.getFeedBackList(startDate,endDate);
        return success(RecruitCompanyConvert.INSTANCE.convertList1(feedBackDoList));
    }

    @PostMapping("/getDistribution")
    @ApiOperation("获得招聘和学员分布数据")
    @TenantIgnore
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "startDate",value ="开始时间",dataType ="String" ),
            @ApiImplicitParam(paramType = "query",name = "endDate",value ="结束时间",dataType ="String" )
    })
    public CommonResult<Distribution> getDistribution( String startDate, String endDate) {
        Distribution distribution = recruitCompanyService.getDistribution(startDate,endDate);
        return success(distribution);
    }

    @PostMapping("/getStudentInfoCount")
    @ApiOperation("运营-招聘统计(学生状况)")
    @TenantIgnore
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "type",value ="查询类型,0:默认(不传时间),1:时间间隔,2:本周,3:本月",dataType ="int",required = true),
            @ApiImplicitParam(paramType = "query",name = "startDate",value ="开始时间",dataType ="string" ),
            @ApiImplicitParam(paramType = "query",name = "endDate",value ="结束时间",dataType ="string" )
    })
    public CommonResult<Distribution> getStudentInfoCount(Integer type, String startDate,String endDate) {
        Distribution studentInfoCount = recruitCompanyService.getStudentInfoCount(NumberUtils.getInteger(type,0), startDate, endDate);
        return success(studentInfoCount );
    }

    @GetMapping("/allCompanyList")
    @ApiOperation("获得招聘企业分页")
    @TenantIgnore
    public CommonResult<List<RecruitCompanyListVO>> selectAllCompanyList() {
        List<CompanyDO> pageResult = recruitCompanyService.getNoShowCompanyList();
        return success(RecruitCompanyConvert.INSTANCE.convertList2(pageResult));
    }

    @GetMapping("/isNoshowCompany")
    @ApiOperation("是否是未发布招聘企业")
    @TenantIgnore
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<Boolean> isNoshowCompany(@RequestParam("id") Long id) {
        Boolean result = recruitCompanyService.isNoshowCompany(id);
        return success(result);
    }

}
